﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using RapidRepository.TestMobileApplication.Entities;
using RapidRepository.Context;
using RapidRepository.TestMobileApplication.Repositories;

namespace RapidRepository.TestMobileApplication
{
    public partial class AddCustomer : PhoneApplicationPage
    {
        public AddCustomer()
        {
            InitializeComponent();
        }

        private void SaveCustomer(object sender, RoutedEventArgs e)
        {
            if (Validate())
            {
                CustomerRepository customerRepository = new CustomerRepository();

                Customer customer = new Customer()
                {
                    FirstName = FirstName.Text,
                    LastName = LastName.Text,
                    Address = new Address() { HouseNumber = int.Parse(HouseNumber.Text), PostCode = PostalCode.Text },
                };

                customer.ContactNumbers.Add(new ContactNumber() { Number = LandLineNumber.Text, ContactNumberType = ContactNumberType.Landline });
                customer.ContactNumbers.Add(new ContactNumber() { Number = MobileNumber.Text, ContactNumberType = ContactNumberType.Mobile });

                customerRepository.Add(customer);

                MessageBox.Show(string.Format("The new customer {0} {1} has been saved successfully", FirstName.Text, LastName.Text), "Success", MessageBoxButton.OK);
                
                NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
            }
        }

        private bool Validate()
        {
            if (string.IsNullOrEmpty(FirstName.Text))
            {
                MessageBox.Show("First name cannot be empty");
                return false;
            }

            if (string.IsNullOrEmpty(LastName.Text))
            {
                MessageBox.Show("Last name cannot be empty");
                return false;
            }

            int houseNumber;
            if (string.IsNullOrEmpty(HouseNumber.Text) || int.TryParse(HouseNumber.Text, out houseNumber) == false)
            {
                MessageBox.Show("House number requires an integer");
                return false;
            }

            if (string.IsNullOrEmpty(PostalCode.Text))
            {
                MessageBox.Show("PostCode cannot be empty");
                return false;
            }

            if (string.IsNullOrEmpty(LandLineNumber.Text))
            {
                MessageBox.Show("Landline number cannot be empty");
                return false;
            }

            if (string.IsNullOrEmpty(MobileNumber.Text))
            {
                MessageBox.Show("MobileNumber cannot be empty");
                return false;
            }

            return true;
        }
    }
}